<?php
# 统计网站情况 文章数，回复数、评论数等。。
function dh_site_stastics()
{
     $cached = get_option('dh_site_stastics');//从options表中获取已缓存的最新留言
     if($cached){    //如果最新留言已缓存，直接显示，否则获取最新留言并缓存到options表中。
         echo $cached; 
     }else{
         $cached = dh_get_site_stastics();
         echo "更新缓存";
         echo $cached;
     }
 }

# 统计特定分类的文章个数
if (!function_exists('wt_get_category_count')) 
{
	function wt_get_category_count($input = '') {
		global $wpdb;
		if($input == '') {
			$category = get_the_category();
			return $category[0]->category_count;
		}
		elseif(is_numeric($input)) {
			$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
			return $wpdb->get_var($SQL);
		}
		else {
			$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
			return $wpdb->get_var($SQL);
		}
	}
}
# 统计本网站能收集到的统计信息
if (!function_exists('dh_get_site_stastics')) 
{
	function dh_get_site_stastics(){
		global $wpdb;
		# 1、日志总数：
		$count_posts = wp_count_posts(); 
		# 发表的日志：
		$published_posts = $count_posts->publish;
		# 未发表的文章，即草稿数目：
		#$draft_posts = $count_posts->draft;
		# 2、评论总数：
		$comments_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");
		# 3、成立时间：
		$live_time = floor((time()-strtotime("2011-01-01"))/86400); 
		# 4、标签总数：
		$count_tags = wp_count_terms('post_tag');
		# 5、页面总数：
		# $count_pages = wp_count_posts('page');
		# $page_posts = $count_pages->publish;
		# 6、分类总数：
		$count_categories = wp_count_terms('category');
		# 7、链接总数：
		# $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'");
		# 8、用户总数：
		#users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
		# 9、最后更新：
		$lastest_update = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts");
		$lastest_update = date('m-d', strtotime($lastest_update[0]->MAX_m));
		# 10、最多阅读：借助于my-visitors-cn插件
		$views_count = get_totalviews(0);
		
		$output = '<ul>';
		$output = $output.'<li>运行时间: <span style="font-size:12px;color:#555;">'.$live_time.'天</span></li>';
		$output = $output.'<li>最近更新: <span style="font-size:12px;color:#555;">'.$lastest_update.'</span></li>';
		$output = $output.'<li>日志总数: <span style="font-size:12px;color:#555;">'.$published_posts.'篇</span></li>';
		$output = $output.'<li>评论总数: <span style="font-size:12px;color:#555;">'.$comments_count.'</span></li>';
		$output = $output.'<li>标签总数: <span style="font-size:12px;color:#555;">'.$count_tags.'个</span></li>';
		$output = $output.'<li>分类总数: <span style="font-size:12px;color:#555;">'.$count_categories.'个</span></li>';
		$output = $output.'<li>阅读总数: </li>';
		$output = $output.'<li><span style="font-size:12px;color:#555;">'.$views_count.'</span></li>';
		$output = $output.'</ul>';
		
		//更新缓存在options表中的最新统计结果
		update_option('dh_site_stastics', $output); 
		return $output;
	}
}

//哪些情况下会更新缓存
add_action('comment_post', 'dh_get_site_stastics');

function dh_posts_orderby_comments()
{
     $cached = get_option('dh_posts_orderby_comments');//从options表中获取已缓存的最新留言
     if($cached){    //如果最新留言已缓存，直接显示，否则获取最新留言并缓存到options表中。
         $cached = posts_orderby_comments();
		 echo $cached; 
     }else{
         $cached = posts_orderby_comments();
         echo $cached;
     }
 }

# 统计最多评论的文章
if (!function_exists('posts_orderby_comments')) 
{
	function posts_orderby_comments() 
	{
	    $get_total=5;
		$before_title='<span class="title">';
		$after_title='</span>&nbsp;';
		$before_right='<span class="right_count">';
		$after_right='</span>';
		global $wpdb;
		$request="SELECT ID,post_title,comment_count FROM $wpdb->posts ORDER BY comment_count DESC LIMIT $get_total";
		$posts=$wpdb->get_results($request);
		$output = '<ul>'."\n";	
		if ($posts) {
			foreach ($posts as $post) {
				$post_title = $post->post_title;
				$comment_count = $post->comment_count;
				$permalink=get_permalink($post->ID);
				$output .= '<li>';
				$output .= $before_title .'<a href="'.$permalink.'" title="'.$post_title.'"><span class="smaller">►</span> '.$post_title.'</a>' .$after_title .$before_right.$comment_count .$after_right ."\n";
				$output .= '</li>';
			}
		}	
		$output .= '</ul>'."\n";
		update_option('dh_posts_orderby_comments', $output); 
		return $output;
	}
}

add_action('comment_post', 'posts_orderby_comments');
add_action('edit_comment', 'posts_orderby_comments');
?>